package com.stadium.controller;

import java.util.List;
import java.util.UUID;

import com.stadium.common.BaseResponse;
import com.stadium.common.enums.ResultEnum;
import com.stadium.common.utils.Md5Utils;
import com.stadium.dto.GamePlayerDto;
import com.stadium.dto.SysUserDto;
import com.stadium.dto.UserMessageDto;
import com.stadium.service.SysUserSV;
import com.stadium.service.UserMessageSV;
import com.stadium.vo.SysUserVo;
import com.stadium.vo.searchVo.SysUserSearchVo;
import com.stadium.vo.searchVo.UserMessageSearchVo;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 用户表
 *
 * @author wsl
 * @email
 * @date 2021-05-11 14:58:57
 */
@RestController
@RequestMapping("/api/sysuser")
@Api(tags = "用户管理")
public class SysUserCL{
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private SysUserSV sysUserSV;
    @Autowired
    private UserMessageSV userMessageSV;

    @ApiOperation(value = "保存或修改")
    @PostMapping(value = "/v1/save")
    public BaseResponse save(@Validated SysUserVo sysUserVo) {
        try {
            if (sysUserVo.getId() == null ||sysUserVo.getId() == ""){
                    sysUserSV.save(sysUserVo);
            }else{
                    sysUserSV.update(sysUserVo);
            }
            return BaseResponse.ok();

        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/v1/delete")
    @ApiImplicitParams({@ApiImplicitParam(name="id",value="id")})
    public BaseResponse delete(@RequestParam(name = "id", required = true)String id) {
        try {
            sysUserSV.delete(id);
            return BaseResponse.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }


    @ApiOperation(value = "获得列表")
    @GetMapping(value = "/v1/query")
    public BaseResponse<List<SysUserDto>> query(@Validated SysUserSearchVo searchVo) {
        try {
            if (StringUtils.isNotEmpty(searchVo.getSearchValue())) {
                searchVo.setSearchValue("%" + searchVo.getSearchValue() + "%");
            }
            if (StringUtils.isNotEmpty(searchVo.getPhoneNum())) {
                searchVo.setPhoneNum(searchVo.getPhoneNum()+ "%");
            }
            List<SysUserDto> queryList = sysUserSV.query(searchVo);
            int total = sysUserSV.getCount(searchVo);
            return BaseResponse.ok(queryList, total);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "获得详情", notes = "获得详情")
    @GetMapping(value = "/v1/get")
    @ApiImplicitParams({@ApiImplicitParam(name="id",value="id")})
    public BaseResponse<SysUserDto> get(@RequestParam(name = "id", required = true)String id) {
        try {
                SysUserDto queryData = sysUserSV.get(id);
            return BaseResponse.ok(queryData);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "根据身份证号获取运动员信息", notes = "根据身份证号获取运动员信息")
    @GetMapping(value = "/v1/getUserByIdNoOrPhoneNum")
    @ApiImplicitParams({@ApiImplicitParam(name = "searchValue", value = "searchValue", required = true)})
    public BaseResponse<List<GamePlayerDto>> getUserByIdNo(@RequestParam(name = "searchValue", required = true) String searchValue) {
        try {
            List<GamePlayerDto> queryData = sysUserSV.getUserByIdNo(searchValue);
            return BaseResponse.ok(queryData);

        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "修改密码")
    @PostMapping("/v1/change_password")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用户id", required = true),
            @ApiImplicitParam(name = "oldPassword", value = "老密码", required = true),
            @ApiImplicitParam(name = "newPassword", value = "新密码", required = true)})
    public BaseResponse changePassword(@RequestParam(name = "id", required = true) String id,
                                       @RequestParam(name = "oldPassword", required = true) String oldPassword,
                                       @RequestParam(name = "newPassword", required = true) String newPassword) {
        try {
            SysUserDto sysUserDto = sysUserSV.get(id);
            if (sysUserDto.getPassword().equals(Md5Utils.hash(oldPassword))) {
                sysUserSV.changePassword(id, Md5Utils.hash(newPassword));
                return BaseResponse.ok();
            } else {
                return BaseResponse.error(ResultEnum.PASSWORD_ERROR);
            }
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }

    }


    @ApiOperation(value = "我的消息", notes = "我的消息")
    @GetMapping(value = "/v1/myMessage")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "userId", required = true)})
    public BaseResponse<List<UserMessageDto>> myMessage(UserMessageSearchVo searchVo) {
        try {
            List<UserMessageDto> query = userMessageSV.query(searchVo);
            int total = userMessageSV.getCount(searchVo);
            return BaseResponse.ok(query,total);

        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }
}
